<template>
  <div>
    <!-- 增加查询重置组件 -->
    <!-- inline="true" 表单一行显示 -->
    <el-form
      ref="searchForm"
      :inline="true"
      :model="searchMap"
      class="demo-form-inline"
      style="margin-left:60px"
    >
      <el-form-item prop="name">
        <el-input v-model="searchMap.name" placeholder="商品名称"></el-input>
      </el-form-item>

      <el-form-item prop="code">
        <el-input v-model="searchMap.code" placeholder="商品编码"></el-input>
      </el-form-item>
      <el-form-item prop="supplierName">
        <!-- 注意click后面添加.native才会监听的到点击事件 -->
        <el-input
          readonly
          @click.native="dialogSuppliervisible=true"
          v-model="searchMap.supplierName"
          placeholder="供应商名称"
        ></el-input>
      </el-form-item>

      <el-form-item style="margin-left:40px">
        <el-button type="primary" @click="fetchData">查询</el-button>
        <el-button type="primary" @click="addMember">新增</el-button>
        <el-button @click="resetForm('searchForm')">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格组件 -->
    <el-table :data="list" height="435" border style="width:100%">
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column prop="name" label="商品名称"></el-table-column>
      <el-table-column prop="code" label="商品编码"></el-table-column>
      <el-table-column prop="spec" label="商品规格"></el-table-column>
      <el-table-column prop="retailPrice" label="零售价"></el-table-column>
      <el-table-column prop="purchasePrice" label="进货价"></el-table-column>
      <el-table-column prop="storageNum" label="库存数量"></el-table-column>
      <el-table-column prop="supplierName" label="供应商"></el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)">修改</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10,20,30]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="margin-left:60px"
    ></el-pagination>
    <!-- 查询时选择供应商对话框组件-->
    <el-dialog title="选择供应商" :visible.sync="dialogSuppliervisible" width="500px">
      <supplier :isDialog="true" @option-supplier="optionSupplier"></supplier>
    </el-dialog>
    <!-- 商品编辑对话框组件 -->
    <el-dialog title="商品编辑" :visible.sync="dialogEditFormVisible" width="500px">
      <el-form
        :rules="rules"
        :model="pojo"
        ref="form"
        label-width="100px"
        label-position="right"
        style="width:400px"
      >
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="pojo.name"></el-input>
        </el-form-item>
        <el-form-item label="商品编码" prop="code">
          <el-input v-model="pojo.code"></el-input>
        </el-form-item>

        <el-form-item label="商品规格" prop="spec">
          <el-input v-model="pojo.spec"></el-input>
        </el-form-item>
        <el-form-item label="零售价" prop="retailPrice">
          <el-input v-model="pojo.retailPrice"></el-input>
        </el-form-item>
        <el-form-item label="进货价" prop="purchasePrice">
          <el-input v-model="pojo.purchasePrice"></el-input>
        </el-form-item>
        <el-form-item label="库存数量" prop="storageNum">
          <el-input v-model="pojo.storageNum"></el-input>
        </el-form-item>
        <el-form-item label="供应商名称" prop="supplierName">
          <el-input
            v-model="pojo.supplierName"
            placeholder="选择供应商"
            readonly
            @click.native="editOptionSupplier"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditFormVisible = false">取 消</el-button>

        <el-button
          type="primary"
          :plain="true"
          @click="pojo.id===null ? addGoodsMsg('form'):updateGoodsMsg('form')"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//导入调用接口api集合
import goodsApi from "@/api/goods";
//导入供应商子组件实例对象
import Supplier from "@/views/supplier";

export default {
  data() {
    return {
      list: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      //与查询表单进行数据双向绑定
      searchMap: {
        name: "",
        code: "",
        supplierName: ""
      },
      dialogSuppliervisible: false,
      //与编辑窗口表单进行数据双向绑定
      pojo: {
        id:null,
        name: "",
        code: "",
        spec: "",
        retailPrice: null,
        purchasePrice: null,
        storageNum: null,
        supplierName: ""
      },
      dialogEditFormVisible: false, //编辑商品
      rules: {
        //检准表单输入规则
        name: [
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
        code: [
          { required: true, message: "商品编码不能为空", trigger: "blur" }
        ],
        retailPrice: [
          { required: true, message: "零售价不能为空", trigger: "blur" }
        ],
        supplierName: [
          { required: true, message: "供应商不能为空", trigger: "change" }
        ]
      },
      isEdit: false //如果为true则是编辑窗口中的表单 false话则是查询选择表单
    };
  },
  components: {
    Supplier // 'supplier':Supplier;
  },

  created() {
    this.fetchData();
  },
  methods: {
    //抓取数据
    fetchData() {
      goodsApi
        .getList(this.currentPage, this.pageSize, this.searchMap)
        .then(response => {
          const resGoodsListData = response.data;
          this.list = resGoodsListData.data.rows;
          this.total = resGoodsListData.data.total;
        });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    //新增商品数据
    addMember() {
      this.dialogEditFormVisible = true;
      this.$nextTick(() => {
        this.$refs["form"].resetFields();
      });
    },
    //重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 查询的时候选择供应商 当选择供应商的时候就触发
    optionSupplier(currentRow) {
      //表示为编辑窗口表单
      if (this.isEdit) {
        this.pojo.supplierName = currentRow.name;

        //表示为查询时表单
      } else {
        // console.log(currentRow);
        this.searchMap.supplierName = currentRow.name;
        this.searchMap.id = currentRow.id;
      }
      this.dialogSuppliervisible = false;
      this.isEdit = false;
    },
    // 新增商品数据 编辑选择供应商
    editOptionSupplier() {
      this.dialogSuppliervisible = true;
      this.isEdit = true;
    },
    //添加商品信息
    addGoodsMsg(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          goodsApi.add(this.pojo).then(response => {
            const addedResponseData = response.data;
            //信息添加成功
            if (addedResponseData.flag) {
              this.$message({
                message: addedResponseData.message,
                type: "success"
              });
              this.fetchData();
              this.dialogEditFormVisible = false;
              //信息添加失败的话
            } else {
              this.$message.error(addedResponseData.message);
              this.fetchData();
              this.dialogEditFormVisible = true;
            }
          });
        } else {
          return false;
        }
      });
    },
    //更新商品信息
    updateGoodsMsg(formName) {
      this.$refs[formName].validate(valid => {
        //校验通过
        if (valid) {
          goodsApi.updateGoodsMsg(this.pojo.id, this.pojo).then(response => {
            const updatedResponseMsg = response.data;
            //更新成功
            if (updatedResponseMsg.flag) {
              this.$message({
                message: updatedResponseMsg.message,
                type: "success"
              });
              this.fetchData();
              this.dialogEditFormVisible = false;
            } else {
              this.$message.error(updatedResponseMsg.message);
              this.fetchData();
              this.dialogEditFormVisible = true;
            }
          });
          // 校验未通过
        } else {
          return false;
        }
      });
     this.pojo.id=null;
    },

    //获取商品信息
    handleEdit(id) {
      this.dialogEditFormVisible = true;
      goodsApi.getGoodsMsg(id).then(response => {
        const getResponseByID = response.data;
        //查询成功
        if (getResponseByID.flag) {
          this.pojo = getResponseByID.data;
          //查询失败
        } else {
          this.addMember();
          this.$message.error(getResponseByID.message);
        }
      });
    },
    //删除商品信息
    handleDelete(id){
            this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          goodsApi.delGoodsData(id).then(response => {
            const deletedResponseMessage = response.data.message;
            //如果删除成功
            if (response.data.flag) {
              this.$message({
                message: deletedResponseMessage,
                type: "success"
              });
              this.fetchData();
              //如果删除失败
            } else {
              this.$message({
                showClose: true,
                message: deletedResponseMessage,
                type: "error"
              });
              this.fetchData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>